home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / AEUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  8.1 KB  |  284 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         MacShell
  5. ** File:         AEUtils.c
  6. ** Written by:   Keith Rollin
  7. ** Modified by:  Eric Soldan
  8. **
  9. ** Copyright © 1990-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13.  
  14.  
  15. /*****************************************************************************/
  16.  
  17.  
  18.  
  19. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  20. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  21. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  22.  
  23.  
  24.  
  25. /*****************************************************************************/
  26.  
  27.  
  28.  
  29. extern Boolean        gHasPPCToolbox;
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34. /*****************************************************************************/
  35.  
  36.  
  37.  
  38. /* DoHighLevelEvent
  39. **
  40. ** Simply calls AEProcessAppleEvent and reports any errors.
  41. ** AEProcessAppleEvent looks in its table of registered events and sees if
  42. ** the current event is registered.  If so, it calls the routine associated
  43. ** with that event.  In our case, we set that to DispatchAppleEvent.
  44. ** DispatchAppleEvent handles it in all cases.  DispatchAppleEvent uses the
  45. ** refCon to determine which AppleEvent it is doing, since it gets them all.
  46. */
  47.  
  48. #pragma segment AppleEvents
  49. void    DoHighLevelEvent(EventRecord *event)
  50. {
  51.     AEProcessAppleEvent(event);
  52. }
  53.  
  54.  
  55.  
  56. /*****************************************************************************/
  57.  
  58.  
  59.  
  60. /* GetTargetInfo
  61. **
  62. ** This function returns the zone, machine, and application name for the
  63. ** indicated target descriptor.
  64. */
  65.  
  66. #pragma segment AppleEvents
  67. OSErr    GetTargetInfo(AEAddressDesc targetDesc, StringPtr zone,
  68.                       StringPtr machine, StringPtr application)
  69. {
  70.     ProcessSerialNumber targetPSN;
  71.     PortInfoRec            portInfo;
  72.     TargetID            theTargetID;
  73.     OSErr                err;
  74.  
  75.     zone[0]        = 0;
  76.     machine[0]     = 0;
  77.     application[0] = 0;
  78.     err = noErr;
  79.  
  80.     if (targetDesc.descriptorType == typeProcessSerialNumber) {
  81.         targetPSN = **(ProcessSerialNumber **)(targetDesc.dataHandle);
  82.         err = GetPortNameFromProcessSerialNumber(&portInfo.name, &targetPSN);
  83.         if (!err) pstrcpy((char *)application, (char *)portInfo.name.name);
  84.         return(err);
  85.     }
  86.  
  87.     if (targetDesc.descriptorType == typeTargetID) {
  88.         theTargetID = **(TargetID **)(targetDesc.dataHandle);
  89.         switch (theTargetID.location.locationKindSelector) {
  90.             case ppcNoLocation:
  91.                 break;
  92.             case ppcNBPLocation:
  93.                 pstrcpy((char *)zone,    (char *)theTargetID.location.u.nbpEntity.zoneStr);
  94.                 pstrcpy((char *)machine, (char *)theTargetID.location.u.nbpEntity.objStr);
  95.                 break;
  96.             case ppcNBPTypeLocation:
  97.                 break;
  98.         }
  99.         pstrcpy((char *)application, (char *)theTargetID.name.name);
  100.         return(noErr);
  101.     }
  102.  
  103.     return(errAEWrongDataType);
  104. }
  105.  
  106.  
  107.  
  108. /*****************************************************************************/
  109.  
  110.  
  111.  
  112. /* MakeTarget
  113. **
  114. ** Creates a TargetID.
  115. **
  116. ** If sendDirect is TRUE, the target is specified by setting a
  117. ** ProcessSerialNumber to kCurrentProcess.  This has the advantage of sending
  118. ** the message directly to ourselves, bypassing ePPC and gaining about a 10-15x
  119. ** speed improvement.  If sendDirect is FALSE, we see if we have the
  120. ** PPCToolBox.  If not, then we are forced to do a direct send.  If we do have
  121. ** the PPCToolbox, then we call PPCBrowser.  We then look at the reply, and
  122. ** factor in the mode we are going to use in AESend.  If that mode is
  123. ** kAEWaitReply and the user selected us as the target, we have to turn that
  124. ** into a direct send.  This is because the AppleEvent Manager will otherwise
  125. ** post the event as a high-level event.  However, we are busy waiting for a
  126. ** reply, not looking for events, so we'll hang.  We avoid this by forcing a
  127. ** direct send.
  128. */
  129.  
  130. #pragma segment AppleEvents
  131. OSErr    MakeTarget(AEAddressDesc *target, Boolean sendDirect, short replyMode,
  132.                    Str255 prompt, Str255 applListLabel,
  133.                    PPCFilterProcPtr portFilter,
  134.                    char *theLocNBPType)
  135. {
  136.     OSErr                    err;
  137.     ProcessSerialNumber     targetPSN;
  138.     ProcessSerialNumber     myPSN;
  139.     TargetID                theTargetID;
  140.     Boolean                    sendingToSelf;
  141.  
  142.     static LocationNameRec    location;
  143.     static PortInfoRec        portInfo;
  144.     static Boolean            defaultOK = false;
  145.  
  146.     err = noErr;    /* Make sure we do the code for the second main if. */
  147.  
  148.     target->dataHandle = nil;
  149.         /* Assume we will fail and nil this descriptor out. */
  150.  
  151.     if (!sendDirect) {
  152.         if (!gHasPPCToolbox)
  153.             sendDirect = true;    /* No tools to send with, so send direct. */
  154.  
  155.         else {        /* We are not sending to self. */
  156.                     /* sendDirect is false.           */
  157.             err = PPCBrowser(
  158.                 prompt,            /* Browse dialog box prompt.           */
  159.                 applListLabel,    /* The 'programs' list title.           */
  160.                 defaultOK,        /* Initially false.                       */
  161.                 &location,        /* Correct if defaultOK is true.       */
  162.                 &portInfo,        /* Correct if defaultOK is true.       */
  163.                 portFilter,        /* Port filtering.                       */
  164.                 (StringPtr)theLocNBPType    /* List ports of this type */
  165.             );
  166.  
  167.             if (!err) {                    /* If user didn't cancel... */
  168.                 defaultOK = true;        /* Default to the same port next time. */
  169.                 if (replyMode == kAEWaitReply) {
  170.                     /* Sender wants a reply and will be waiting... */
  171.  
  172.                     sendingToSelf = false;
  173.                         /* Assume that we aren't sending to ourselves. */
  174.  
  175.                     if (!location.locationKindSelector) {
  176.                         /* Hey, we are sending to ourselves! */
  177.  
  178.                         err = GetProcessSerialNumberFromPortName(
  179.                                 &portInfo.name, &targetPSN);
  180.                         if (!err) {
  181.                             GetCurrentProcess(&myPSN);
  182.                             err = SameProcess(&targetPSN, &myPSN, &sendingToSelf);
  183.                         }
  184.                     }
  185.  
  186.                     if (sendingToSelf) sendDirect = true;
  187.  
  188.                 }
  189.             }
  190.         }
  191.     }
  192.  
  193.     if (!err) {
  194.         if (sendDirect) {
  195.             /* Finally, we get to the point... */
  196.  
  197.             targetPSN.highLongOfPSN = 0;
  198.             targetPSN.lowLongOfPSN = kCurrentProcess;
  199.                 /* Process serial # is equal to kCurrentProcess.  This
  200.                 ** bypasses ePPC and speeds up things considerably. */
  201.  
  202.             err = AECreateDesc(
  203.                 typeProcessSerialNumber,    /* Standard PSN descriptor type. */
  204.                 (Ptr)&targetPSN,            /* "No ePPC" process serial #.     */
  205.                 sizeof(targetPSN),            /* Size of data (2 longs).         */
  206.                 target                        /* Wherefore art thou desc.         */
  207.             );
  208.         }
  209.         else {
  210.             theTargetID.location = location;
  211.             theTargetID.name     = portInfo.name;
  212.                 /* The fields sessionID does not need to be filled in now.
  213.                 ** The sessionID is returned when you actually connect to
  214.                 ** a port.  You can then use the sessionID from that point
  215.                 ** on to improve speed.
  216.                 **
  217.                 ** You also don't need to fill in the recvrName field at this
  218.                 ** point.  This is filled in, again, when the session is
  219.                 ** actually established.
  220.                 **
  221.                 ** The amount of data for a non-us target is bigger.
  222.                 ** We need the whole dealie for our target, since
  223.                 ** it is out on the net somewhere. */
  224.  
  225.             err = AECreateDesc(
  226.                 typeTargetID,            /* Standard target descriptor type. */
  227.                 (Ptr)&theTargetID,        /* The data for the descriptor.        */
  228.                 sizeof(theTargetID),    /* Size of the data.                */
  229.                 target                    /* Wherefore art thou desc.            */
  230.             );
  231.         }
  232.     }
  233.     return (err);
  234. }
  235.  
  236.  
  237.  
  238. /*****************************************************************************/
  239.  
  240.  
  241.  
  242. /* MissedAnyParameters
  243. **
  244. ** Used to check for any unread required parameters. Returns true if we
  245. ** missed at least one.
  246. */
  247.  
  248. #pragma segment AppleEvents
  249. Boolean    MissedAnyParameters(AppleEvent *message)
  250. {
  251.     OSErr        err;
  252.     DescType    ignoredActualType;
  253.     AEKeyword    missedKeyword;
  254.     Size        ignoredActualSize;
  255.     EventRecord    event;
  256.  
  257.     err = AEGetAttributePtr(    /* SEE IF PARAMETERS ARE ALL USED UP.          */
  258.         message,                /* AppleEvent to check.                          */
  259.         keyMissedKeywordAttr,    /* Look for unread parameters.                  */
  260.         typeKeyword,            /* So we can see what type we missed, if any. */
  261.         &ignoredActualType,        /* What is would have been if not coerced.      */
  262.         (Ptr)&missedKeyword,    /* Data area.  (Keyword not handled.)          */
  263.         sizeof(missedKeyword),    /* Size of data area.                          */
  264.         &ignoredActualSize        /* Actual data size.                          */
  265.     );
  266.  
  267. /* No error means that we found some unused parameters. */
  268.  
  269.     if (err == noErr) {
  270.         event.message = *(long *) &ignoredActualType;
  271.         event.where = *(Point *) &missedKeyword;
  272.         err = errAEEventNotHandled;
  273.     }
  274.  
  275. /* errAEDescNotFound means that there are no more parameters.  If we get
  276. ** an error code other than that, flag it.
  277. */
  278.  
  279.     return(err != errAEDescNotFound);
  280. }
  281.  
  282.  
  283.  
  284.